package com.yjd.pub.merchant.service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.funds.enums.FundsIncomeEnum;
import com.yjd.comm.merchant.enums.MerchantFundRefTabEnum;
import com.yjd.comm.merchant.enums.MerchantFundsTypeEnum;
import com.yjd.comm.merchant.model.MerchantCashModel;
import com.yjd.comm.merchant.model.MerchantFundsModel;
import com.yjd.comm.merchant.service.IMerchantCashService;
import com.yjd.comm.merchant.service.IMerchantFundsDataService;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.pub.merchant.dao.IMerchantCashDao;
import com.yjd.pub.merchant.dao.IMerchantFundsDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <b>description</b>：商户提现记录表业务实现<br>
 * <b>time</b>：2017-10-19 15:50:41 <br>
 * <b>author</b>：  carl
 */
@Service("merchantCashService")
public class MerchantCashServiceImpl extends BaseServiceImpl implements IMerchantCashService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public MerchantCashModel insert(MerchantCashModel model) throws Exception {
		return this.merchantCashDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(MerchantCashModel model) throws Exception {
		return this.merchantCashDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.merchantCashDao.deleteModel(paramMap, true);
	}

	@Override
	public void merchantCashOut(MerchantCashModel cash) throws Exception {

		MerchantFundsModel merchantFundsAccount = merchantFundsDao.getModelById(cash.getOrg_id(), true);
		if(null == merchantFundsAccount){
			FrameUtil.throwBaseException1("商户账户不存在");
		}

		if(merchantFundsAccount.getAvailable().compareTo(cash.getPrice()) < 0){
			FrameUtil.throwBaseException1("提现金额大于账户余额");
		}

		Long current = FrameUtil.getTime();
		cash.setCreate_time(current);
		cash.setUpdate_time(current);
		cash.setStatus(1);
		MerchantCashModel cashModel = merchantCashDao.insertModel(cash, true);
		if(null == cashModel){
			FrameUtil.throwBaseException1("添加提现记录失败");
		}

		// 记录商户资金变更：累计提现++，账户可用余额--
		Double available = ArithUtil.sub(merchantFundsAccount.getAvailable(), cash.getPrice());
		int updateCount = merchantFundsDao.updateByMap(FrameUtil.newHashMap(
				"org_id", merchantFundsAccount.getOrg_id(),
				"where_version", merchantFundsAccount.getVersion(),
				"cash", ArithUtil.add(merchantFundsAccount.getCash(), cash.getPrice()),
				"available", available), true);
		if(updateCount != 1){
			FrameUtil.throwBaseException1("更新商户资金失败");
		}

		// 记录流水
		merchantFundsDataService.insertFundsData(merchantFundsAccount.getOrg_id(), cash.getPrice(), available, FundsIncomeEnum.REDUCE,
				MerchantFundsTypeEnum.CASH,cashModel.getId().toString(), MerchantFundRefTabEnum.MFQ_MERCHANT_CASH,current);
	}

	@Override
	public Double querySumCash(Long startTime, Long endTime) throws Exception {
		if(startTime==null||endTime==null){
			FrameUtil.throwBaseException1("请求参数不能为空");
		}
		return merchantCashDao.querySumCash(FrameUtil.newHashMap("startTime",startTime,"endTime",endTime));
	}

	@Resource
	private IMerchantFundsDao merchantFundsDao;

	@Resource
	private IMerchantFundsDataService merchantFundsDataService;

	@Resource
	private IMerchantCashDao merchantCashDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.merchantCashDao;
	}

}
